package com.sav.common.controller;
 

import java.io.File;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sav.common.catalog.Product;
import com.sav.common.catalog.Category;
import com.sav.common.dao.CategoryDAO;
import com.sav.common.dao.ProductDAO;
import com.sav.common.forms.ProductForm;

    
@Controller
public class ProductController { 
    
    @Autowired(required = true)
    private ProductDAO productDAO;
    
    @Autowired(required = true)
    private CategoryDAO categoryDAO;
    
    @RequestMapping(value = "/showProduct", method = RequestMethod.POST) 
    public @ResponseBody Product showProduct(
    		@RequestParam(value="id", required=true) Integer idProduct,
    		ModelMap model) throws Exception {

    	Product product = productDAO.getProductById(idProduct);
		//model.addAttribute("product", product);
        return product;
    }
    
    @RequestMapping(value = "/deleteProduct", method = RequestMethod.POST)
    public @ResponseBody void deleteProduct(
    		@RequestParam(value="id", required=true) Integer id,
    		Model model)  throws Exception{
    	
    	productDAO.deleteProduct(id);
    	File file = new File(Consts.filePlaceToUpload + id + ".jpg");
    	if (file.exists()) file.delete();
	}
    
   /* @RequestMapping(value = "/editProduct", method = RequestMethod.GET) 
    public void editProduct(
    		@RequestParam(value="id", required=true) Integer id,
    		ModelMap model) throws Exception {
    	
    	Product product = productDAO.getProductById(id);
    	model.addAttribute("product", product);
    } */
    
 /*   @RequestMapping(value = "/addProduct", method = RequestMethod.GET) 
    public String addProduct(
    		@RequestParam(value="category", required=true) Integer id,
    		ModelMap model) throws Exception {
    	Category category = categoryDAO.getCategoryByID(id);
    	model.addAttribute("category", category);
    	return "addProduct";
    } */
    
    @RequestMapping(value = "/addProduct", method = RequestMethod.GET) 
    public String addProduct(
    		@RequestParam(value="category", required=true) Integer id,
    		ModelMap model) throws Exception {
    	Category category = categoryDAO.getCategoryByID(id);
    	model.addAttribute("category", category);
		ProductForm productForm = new ProductForm();
		model.put("productForm", productForm);
    	return "addProduct";
    } 
    
    @RequestMapping(value = "/editProduct", method = RequestMethod.GET) 
    public String editProduct(
    		@RequestParam(value="id", required=true) Integer id,
    		ModelMap model) throws Exception {

		ProductForm productForm = new ProductForm();
		Product product = productDAO.getProductById(id);
    	model.addAttribute("product", product);
		productForm.fromProduct(product);
		model.put("productForm", productForm);
    	return "editProduct";
    } 
    
}